package com.example.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @date 2019/4/25 11:46
 * @atuther wangbo
 */
public class jwtUtils {

    //密钥
    public static final String SECRET = "sdjhakdhajdklsl;o653632";
    //过期时间:秒
    public static final int EXPIRE = 5;

    private static Long expire = 604800L;// 32位秘钥
    private static String secret = "abcdfghiabcdfghiabcdfghiabcdfghi";
    public static String generateToken(String username) {
        Date now = new Date();
        Date expiration = new Date(now.getTime() + 1000 * expire);
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
        return Jwts.builder()
                .setHeaderParam("type", "JwT")
                .setSubject(username)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .signWith(signatureAlgorithm, secret)
                .compact();
    }



//解析token
    public static Claims getClaimsByToken(String token){
        return Jwts.parser()
                .setSigningKey( secret)
                .parseClaimsJws(token)
                .getBody();
    }


}
